package com.vietnam.power.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vietnam.power.dao.UserDao;
import com.vietnam.power.persistence.UserInfo;

@Repository
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
public class UserDaoImpl extends GenericDaoImpl implements UserDao {

	@Override
	public UserInfo getUserById(Long id) {
		return findById(UserInfo.class, id);
	}

	@Override
	public UserInfo getUserByUserName(String username) {
		Criteria criteria = getActiveSession().createCriteria(UserInfo.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		criteria.add(Restrictions.ilike("username", username, MatchMode.EXACT));

		return (UserInfo) criteria.uniqueResult();
	}

}
